import morgan from 'morgan';
import helmet from 'helmet';
import express from 'express';
import 'express-async-errors';
import EnvVars from '@src/declarations/major/EnvVars';
import {NodeEnvs} from '@src/declarations/enums';
import "@src/models/user";
import "@src/models/jurisdiction";
import "@src/models/dict";
import "@src/models/fieldCategory";
import "@src/models/literature";
import "@src/models/file";
import "@src/models/article";
import "@src/models/region";
import sequelize from "@src/dataBase/db";
import apiRouter from "@src/routes/api";


//初始化
const app = express();

//解析传入的json请求并将解析的数据放在rq.body中
app.use(express.json());
//解析表单中的请求体数据，以json数据返回
app.use(express.urlencoded({extended: true}));

// 当前环境为开发环境
if (EnvVars.nodeEnv === NodeEnvs.Dev) {
  // 同步模型
  sequelize.sync().then(() => {
    // 权限
    // qxServer().then(() => console.log("同步成功"),
    //     err => console.log("错误" + err)
    // )
    // 字典
    // categoryServer().then(()=>console.log("同步成功"),err=>console.log("同步失败："+err))
  });
  // 输出路由日志
  app.use(morgan('dev'));
}

// 当前环境为正式环境，防止xss攻击等
if (EnvVars.nodeEnv === NodeEnvs.Production) {
  app.use(helmet());
}
// api中间件
app.use('/api', apiRouter);

export default app;
